<?php
/**
 * @author 
 * @copyright 2014
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Menu_model extends CI_Model
{
	var $arr_action=array();
	function __construct(){
		parent::__construct('menus');
        $this->arr_action = $this->getTask();                
	}
    function getTask(){ 
        $this->db->select('title,name');
		$this->db->order_by('order');
		$table=$this->db->get('chucnanghethong');

        $data = $table->result_array();
        $menu = array();

        foreach($data as $value)
        {
            $menu[$value['title']] = $value['name'];
        }
       
        return $menu;
    }            
    function getMenu($current = ''){
        if(!empty($current)){
          $current = json_decode($current,true);
		}
        else{
          $current = array();
		}
        $this->db->select('id,title,params');
		$this->db->where('parent',0);
        $this->db->where('title <>','sep');
		$this->db->order_by('ordering');
		$table=$this->db->get('menus');

        $data = $table->result_array();
        $menu = '';
        $i = 0;
        $n = count($data);
        $rchk_c = 0;
        foreach($data as $value)
        {
            $asub = $this->getChildren($value['id'], $current);
            if($asub)
            {
                $sub = $asub['sub'];
                $rchk = $asub['chk'];
                $menu.='{id : "node-'.$value['id'].'",
                            value : "parent-'.$value['id'].'",
                            text : "'.$value['title'].'",
                            showcheck : true,
                            complete : true,
                            isexpand : true,
                            checkstate : '.$rchk.',
                            hasChildren : true,
                            ChildNodes : '.$sub.'
                            }';
                if($rchk==1 || $rchk==2) $rchk_c++;
            }
            else
            {
                $params = explode(',',$value['params']);
                $str_param = '';
                $j =0;
                $m = count($params);
                $l = (isset($current[$value['id']]))?count($current[$value['id']]):0;
                foreach($params as $param)
                {
                    if(isset($value['id']))
                            $va = $value['id'];
                    else
                            $va = '';
                    if(isset($this->arr_action[$param]))
                            $acc = $this->arr_action[$param];
                    else
                            $acc = '';
                    $chk = (isset($current[$value['id']][$param]))?1:0;
                    $str_param.='{id : "node-'.$va.'-'.$param.'",
                                value : "'.$value['id'].'-'.$param.'",
                                text : "'.$acc.'",
                                showcheck : true,
                                complete : true,
                                isexpand : false,
                                checkstate : '.$chk.',
                                hasChildren : false}';
                    if($j<($m-1)) $str_param.=',';
                    $j++;
                }
                
                if($l < 1) $pchk = 0;
                else                
                    $pchk = ($l == $m)?1:2;
                if($pchk==1) $rchk_c++;
                $menu.='{id : "node-'.$value['id'].'",
                            value : "sub-'.$value['id'].'",
                            text : "'.$value['title'].'",
                            showcheck : true,
                            complete : true,
                            isexpand : false,
                            checkstate : '.$pchk.',
                            hasChildren : true,
                            ChildNodes : ['.$str_param.']
                            }';
            }
            if($i<($n-1)) $menu.=',';
            $i++;
        }
        if($rchk_c<1) $rrchk = 0;
        else
            $rrchk = ($rchk_c == $n)?1:2;
        return array('chk'=>$rrchk,'menu'=>'['.$menu.']');
    }
    private function getChildren($id, $current, $level=0){   
        $currentuser = $this->getMenuUser($this->pub->login['id']);
        
        $this->db->select('id,title,parent,params');
        if($this->pub->login['id']!=1)
        {
            $this->db->where('link <>','group');
            $this->db->where('link <>','installer');     
        }
        
        if(count($currentuser)>0){
            $this->db->where_in('id',$currentuser);
        }
        $this->db->where('title <>','sep');
		$this->db->where('parent',$id);
		$this->db->order_by('ordering');
		$table=$this->db->get('menus');
        if($table->num_rows()>0)
        {
            $data = $table->result_array();
            $i = 0;
            $n = count($data);
            $menu = '';
            $rchk_c = 0;
            foreach($data as $value){
                $asub = $this->getChildren($value['id'], $current, 1);
                if($asub){
                    $sub = $asub['sub'];
                    $rchk = $asub['chk'];
                   
                    $menu.='{id : "node-'.$value['id'].'",
                                value : "parentsub-'.$value['id'].'",
                                text : "'.$value['title'].'",
                                showcheck : true,
                                complete : true,
                                isexpand : true,
                                checkstate : '.$rchk.',
                                hasChildren : true,
                                ChildNodes : '.$sub.'
                                }';
                    if($rchk==1 || $rchk==2) $rchk_c++;
                }
                else{
                    $params = explode(',',$value['params']);
                    $str_param = '';
                    $j =0;
                    $m = count($params);
                    $l = (isset($current[$value['id']]))?count($current[$value['id']]):0;
                    foreach($params as $param)
                    {
						$quyen = '';
						if(isset($this->arr_action[$param]))
							$quyen = $this->arr_action[$param];
                        $chk = (isset($current[$value['id']][$param]))?1:0;
                        $parent = (($level>0)?'-'.$value['parent']:'');
                        $str_param.='{id : "node-'.$value['id'].'-'.$param.$parent.'",
                                    value : "'.$value['id'].'-'.$param.$parent.'",
                                    text : "'.$quyen.'",
                                    showcheck : true,
                                    complete : true,
                                    isexpand : false,
                                    checkstate : '.$chk.',
                                    hasChildren : false}';
                        if($j<($m-1)) $str_param.=',';
                        $j++;
                    }
                    if($l < 1) $pchk = 0;
                    else                
                        $pchk = ($l == $m)?1:2;
                        
                    if($pchk>0) $rchk_c++;
                    
                    $menu.='{id : "node-'.$value['id'].'",
                                value : "sub-'.$value['id'].'",
                                text : "'.$value['title'].'",
                                showcheck : true,
                                complete : true,
                                isexpand : true,
                                checkstate : '.$pchk.',
                                hasChildren : true,
                                ChildNodes : ['.$str_param.']
                                }';
                }
                if($i<($n-1)) $menu.=',';
                $i++;
            }
            if($rchk_c<1) $rrchk = 0;
            else
                $rrchk = ($rchk_c == $n)?1:2;
           
            return array('chk'=>$rrchk,'sub'=>'['.$menu.']');
        }
        return false;
    }
    function getMenuUser($user){

        $menuuser = $this->getGroups($user);
        $this->db->select('params');
		$this->db->where_in('id',$menuuser);
		$table=$this->db->get('groups');
        $amenus = array();
        $n  = $table->num_rows();
		print_r($n); exit;
        if($n>0){
            for($i=0; $i<$n; $i++){
               $temp = $table->row_array($i);
                $menus = json_decode($temp['params'],true);
				foreach($menus as $k=>$v){
				   if(!isset($amenus[$k]))
					$amenus[$k] = $k;
				}
           }
        }  
        return $amenus;
    }
    
    function getGroups($user){
        $this->db->select('groups');
		$this->db->where('id',$user);
		$table = $this->db->get('users');
        $table = $table->row_array();
        if(isset($table['groups'])){
            return explode(',',$table['groups']);
		}
    }
 }